﻿
// MobileGameDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MobileGame.h"
#include "MobileGameDlg.h"
#include "afxdialogex.h"
#include "CppSQLite3.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMobileGameDlg 对话框


CMobileGameDlg* g_MobileGameDlg = NULL;

CMobileGameDlg::CMobileGameDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_MOBILEGAME_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMobileGameDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_INFO, m_list_info);
	DDX_Control(pDX, IDC_TAB_WND, m_tab_wnd);
}

BEGIN_MESSAGE_MAP(CMobileGameDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(NM_RCLICK, IDC_LIST_INFO, &CMobileGameDlg::OnNMRClickListInfo)
	ON_COMMAND(ID_Memu_ImportAccount, &CMobileGameDlg::OnMemuImportaccount)
END_MESSAGE_MAP()


// CMobileGameDlg 消息处理程序

//释放资源dll到指定目录
BOOL CMobileGameDlg::ExtraceRes(DWORD res_id, char * path)
{
	HINSTANCE instance = AfxGetInstanceHandle();

	//定位我们的自定义资源，这里因为我们是从本模块定位资源，所以将句柄简单地置为NULL即可
	HRSRC hRsrc = FindResource(instance, MAKEINTRESOURCE(res_id), TEXT("Res"));
	if (NULL == hRsrc)
		return FALSE;

	//获取资源的大小
	DWORD dwSize = SizeofResource(instance, hRsrc);
	if (0 == dwSize)
		return FALSE;

	//加载资源
	HGLOBAL hGlobal = LoadResource(instance, hRsrc);
	if (NULL == hGlobal)
		return FALSE;

	//锁定资源
	LPVOID pBuffer = LockResource(hGlobal);
	if (NULL == pBuffer)
		return FALSE;

	FILE * f = fopen(path, "wb");
	if (f == NULL)
		return FALSE;

	fwrite(pBuffer, 1, dwSize, f);

	fclose(f);

	return TRUE;
}

//初始化Config.ini
void CMobileGameDlg::InitConfig()
{
	CStringArray Sections;

	int iCount = g_AccountFile.GetSections(Sections);
	if (iCount <= 0)
	{
		return;
	}
	m_list_info.DeleteAllItems();


	for (int i = 0; i < iCount; i++)
	{
		int ItemCount = m_list_info.InsertItem(m_list_info.GetItemCount(), NumberToString(i));
		CString strAccount = g_AccountFile.GetString(NumberToString(i + 1), _T("账号"));
		CString strPwd = g_AccountFile.GetString(NumberToString(i + 1), _T("密码"));
		CString strArea1 = g_AccountFile.GetString(NumberToString(i + 1), _T("二密"));
		CString strPro = g_AccountFile.GetString(NumberToString(i + 1), _T("职业"));

		int iRoleIndex = g_AccountFile.GetInt(NumberToString(i + 1), _T("角色"));
		DbgOut("[sdn] 角色:iRoleIndex:%d", iRoleIndex);
		if (iRoleIndex == 0)
		{
			iRoleIndex = 1;
		}
		if (!strPro.IsEmpty())
		{
			m_list_info.SetItemText(ItemCount, 11, strPro);
		}
		int iChengHao = g_AccountFile.GetInt(NumberToString(i + 1), _T("完成"));
		if (!strAccount.IsEmpty() && !strPwd.IsEmpty())// && !strArea.IsEmpty())
		{
			m_list_info.SetItemText(ItemCount, 1, strAccount);
			m_list_info.SetItemText(ItemCount, 2, strPwd);
			if (!strArea1.IsEmpty())
			{
				m_list_info.SetItemText(ItemCount, 3, strArea1);
			}

			if (iRoleIndex != 0)
			{
				//m_list_info.SetItemText(ItemCount, 5, NumberToString(iRoleIndex));
			}
			if (iChengHao == 1)
			{
				m_list_info.SetItemText(ItemCount, 6, L"完成");
			}
			else if (iChengHao == 2)
			{
				m_list_info.SetItemText(ItemCount, 6, L"账号问题");
			}
			else if (iChengHao == 3)
			{
				m_list_info.SetItemText(ItemCount, 6, L"封号");
			}
			else if (iChengHao == 4)
			{
				m_list_info.SetItemText(ItemCount, 6, L"未知");
			}
			else if (iChengHao == 0) {
				m_list_info.SetItemText(ItemCount, 6, L"未完成");
			}
		}
	}
}

BOOL CMobileGameDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();


	g_MobileGameDlg = this;
	CreateDir(_T("C:\\MobileGame"));

	if (!PathFileExists(L"c:\\MobileGame\\dm.dll"))
	{
		ExtraceRes(IDR_RES_DM, "c:\\MobileGame\\dm.dll");
		Sleep(500);
	}
	RegisterGlobalDM(L"c:\\MobileGame\\dm.dll");//初始化大漠插件


	g_IniFile.SetIniFileName(ConfigFile);
	g_AccountFile.SetIniFileName(AccountFile);
	g_RoleNameFile.SetIniFileName(RoleNameFile);
	g_MoneyFile.SetIniFileName(MoneyFile);
	g_TaskSpeedFile.SetIniFileName(TaskSpeedFile);
	g_ChangHaoStateFile.SetIniFileName(ChengHaoStateFile);

	// TODO:  在此添加额外的初始化
	ListView_SetExtendedListViewStyle(m_list_info.GetSafeHwnd(),
		LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP | LVS_EX_GRIDLINES);    //网格线 //LVS_EX_CHECKBOXES
	int nIndex = 0;
	m_list_info.InsertColumn(nIndex++, _T("序号"), LVCFMT_CENTER, 50);//0
	//m_list_info.InsertColumn(nIndex++, _T("句柄"), LVCFMT_CENTER, 60);//1
	m_list_info.InsertColumn(nIndex++, _T("账号"), LVCFMT_LEFT, 70);//2
	m_list_info.InsertColumn(nIndex++, _T("密码"), LVCFMT_LEFT, 70);//3
	m_list_info.InsertColumn(nIndex++, _T("二密"), LVCFMT_CENTER, 70);//4
	m_list_info.InsertColumn(nIndex++, _T("角色序号"), LVCFMT_CENTER, 60);//5
	//m_list_info.InsertColumn(nIndex++, _T("银币"), LVCFMT_CENTER, 60);//6
	//m_list_info.InsertColumn(nIndex++, _T("等级"), LVCFMT_CENTER, 50);//7
	//m_list_info.InsertColumn(nIndex++, _T("模拟器"), LVCFMT_CENTER, 60);//8
	m_list_info.InsertColumn(nIndex++, _T("任务状态"), LVCFMT_CENTER, 150);//9
	m_list_info.InsertColumn(nIndex++, _T("运行状态"), LVCFMT_CENTER, 60);//10
	//m_list_info.InsertColumn(nIndex++, _T("职业"), LVCFMT_CENTER, 80);//11

																	//属性页控件
	m_tab_wnd.AddPage(L"基本设置", &m_Page1, IDD_PAGE1);
	//m_tab_wnd.AddPage("其他设置", &m_Page2, IDD_PAGE2);

	//InitLeiDian();
	InitConfig();
	InitAllocateDM();
	//m_GGameHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
	//int ret = m_GlobalDM->BindWindowEx(m_GGameHwnd, L"gdi", L"normal", L"normal", L"", 0);

	CppSQLite3DB db;
	CFileFind fFind;

	if (fFind.FindFile(_T("C:\\MobileGame\\DMTool.db")))
	{
		db.open("C:\\MobileGame\\DMTool.db", "ppo00123");
	}
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


//获取界面的一些信息
void CMobileGameDlg::InitUIGameInfo(int iControlIndex, GameInfo& _GameInfo)
{
	CString strIndex = m_list_info.GetItemText(iControlIndex, 0);
	_GameInfo.m_strIndex = strIndex;

	_GameInfo.m_strAccount = m_list_info.GetItemText(iControlIndex, 2);
	_GameInfo.m_strPwd = m_list_info.GetItemText(iControlIndex, 3);
	_GameInfo.m_strServer = m_list_info.GetItemText(iControlIndex, 4);
	_GameInfo.m_iRoleIndex = _ttoi(m_list_info.GetItemText(iControlIndex, 5));
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMobileGameDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMobileGameDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMobileGameDlg::OnNMRClickListInfo(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);


	// TODO: 在此添加控件通知处理程序代码
	CPoint ptCurSel(0, 0);
	GetCursorPos(&ptCurSel);
	//转换为当前控件坐标系的坐标
	m_list_info.ScreenToClient(&ptCurSel);

	LVHITTESTINFO HitTestInfo;
	HitTestInfo.pt = ptCurSel;
	HitTestInfo.flags = LVHT_ABOVE;
	//判断坐标命中第几项
	int nItem = m_list_info.SubItemHitTest(&HitTestInfo);

	//如果有选中项
	//弹出菜单
	CMenu menu;
	menu.LoadMenu(IDR_MENU1);
	CPoint point;
	GetCursorPos(&point);
	CMenu* pSubMenu = menu.GetSubMenu(0);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);

	*pResult = 0;
}


void CMobileGameDlg::OnMemuImportaccount()
{
	// TODO: 在此添加命令处理程序代码
	CString strFileName, strPathName;

	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, _T("文本文件(*.txt)|*.txt||"));
	if (dlg.DoModal() == IDOK)
	{
		strFileName = dlg.GetPathName();
		CStdioFileEx sFile;

		CStringArray ArrAcountInfo;
		if (!strFileName.IsEmpty())
		{
			sFile.Open(strFileName, CFile::modeRead);
			CString strLine;
			while (sFile.ReadString(strLine))
			{
				CString strTemp = strLine;
				int len = strLine.GetLength();
				if (len > 50)
				{
					sFile.Close();
					return;
				}
				//zhanghao-mima-daqu
				strLine.Replace(_T("----"), _T("-"));
				strLine.Replace(_T("---"), _T("-"));
				strLine.Replace(_T("--"), _T("-"));

				StringSplit(strLine, ArrAcountInfo, '-');

				int	iBegin = m_list_info.GetItemCount();

				int iRow = m_list_info.InsertItem(iBegin, NumberToString(iBegin));
				m_list_info.SetItemText(iRow, 1, ArrAcountInfo[0]);//瑯뵀
				m_list_info.SetItemText(iRow, 2, ArrAcountInfo[1]);//쵱쯤
				m_list_info.SetItemText(iRow, 3, ArrAcountInfo[2]);//댕혐

				if (!ArrAcountInfo[0].IsEmpty() && !ArrAcountInfo[1].IsEmpty())//&& !ArrAcountInfo[2].IsEmpty())
				{
					g_AccountFile.SetString(NumberToString(iRow + 1), L"账号", ArrAcountInfo[0]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"密码", ArrAcountInfo[1]);
					g_AccountFile.SetString(NumberToString(iRow + 1), L"二密", ArrAcountInfo[2]);
				}

			}
			sFile.Close();
		}
	}
}
